package com.poetry.vup.works.service.impl;

import com.poetry.vup.common.entity.works.VupWorksDO;
import com.poetry.vup.common.exception.KabobExceptionUtil;
import com.poetry.vup.common.result.BaseResult;
import com.poetry.vup.common.result.ResultCode;
import com.poetry.vup.common.result.page.BasePage;
import com.poetry.vup.common.result.page.PageParam;
import com.poetry.vup.common.util.JacksonUtil;
import com.poetry.vup.common.util.OSSClientUtil;
import com.poetry.vup.common.util.PageUtils;
import com.poetry.vup.works.domain.VupWorksDTO;
import com.poetry.vup.works.domain.VupWorksVO;
import com.poetry.vup.works.mapper.WorksExhibitionMapper;
import com.poetry.vup.works.service.WorksExhibitionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WorksExhibitionServiceImpl implements WorksExhibitionService {

    @Autowired
    private WorksExhibitionMapper worksExhibitionMapper;

    @Autowired
    private OSSClientUtil ossClientUtil;

    @Override
    public BasePage<VupWorksVO> findAllWorks(int pageSize, int pageNum) {
        List<VupWorksVO> list = new ArrayList<>();
        BasePage<VupWorksVO> page = new BasePage<>();

        int offset = (pageNum-1) * pageSize;//计算偏移量
        if(offset < 0){
            offset = 1;
        }

        Long count = null;
        list = worksExhibitionMapper.findAllWorks(offset,pageSize);//所有用户列表
        count = worksExhibitionMapper.getCount();

        if(list != null && list.size() != 0){
            return PageUtils.buildBasePage(list,new PageParam(pageNum,pageSize),count);
        }else {
            page.setList(list);
        }

        return page;
    }

    /**
     * 添加作品
     * @param vupWorksDTO
     * @return
     */
    @Override
    public Long addWorks(VupWorksDTO vupWorksDTO) {
        int insert = worksExhibitionMapper.insert(JacksonUtil.objectToObject(vupWorksDTO, VupWorksDO.class));
        return JacksonUtil.objectToObject(vupWorksDTO, VupWorksDO.class).getId();
    }

    /**
     * 上传图片
     * @param file
     * @param id
     * @return
     */
    @Override
    public BaseResult<Object> uploadFile(MultipartFile file, Long id) {
        BaseResult<Object> baseResult = BaseResult.failed("");
        try {
            //上传图片
            String str = ossClientUtil.uploadImg2Oss(file);
            String url = ossClientUtil.getUrl(str);
            //查找属于那个作品的图片
            VupWorksDO vupWorksDO = worksExhibitionMapper.selectById(id);

            vupWorksDO.setWorkCover(url);
            int update = worksExhibitionMapper.updateById(vupWorksDO);
            if(update > 0){
                baseResult = BaseResult.success(update);
            }

        } catch (Exception e) {
            KabobExceptionUtil.throwError(ResultCode.FAILED,"上传图片异常");
        }
        return baseResult;
    }


}
